import javax.imageio.stream.ImageInputStream;
import java.util.List;

public class Test {
    //给你一个数组 nums 和一个值 val，你需要 原地 移除所有数值等于 val 的元素，并返回移除后数组的新长度。

    //不要使用额外的数组空间，你必须仅使用 O(1) 额外空间并 原地 修改输入数组。

    //元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

    public static int find(int[] nums, int toFind, int length) {
        for (int i = 0; i < length; i++) {
            if (nums[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    public static void remove(int[] nums, int index, int length) {
        for (int i = index; i < length - 1; i++) {
            nums[i] = nums[i + 1];
        }

    }

    public int removeElement(int[] nums, int val) {
        int count = 0;
        int length = nums.length;
        while (find(nums, val, length) != -1) {
            count++;
            int index = find(nums, val, length);
            remove(nums, index, length);
            length--;
        }
        return nums.length - count;
    }

    public static int removeDuplicates1(int[] nums) {
        int idx = 0, j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i+1< nums.length&&nums[i] == nums[i +1]) {
                continue;
            } else {
                nums[idx++] = nums[i];
            }
        }
        return idx;
    }
    //输入：s = "Hello how are you Contestant", k = 4
    //输出："Hello how are you"
    //解释：
    //s 中的单词为 ["Hello", "how" "are", "you", "Contestant"]
    //前 4 个单词为 ["Hello", "how", "are", "you"]
    //因此，应当返回 "Hello how are you"
    public static String fun(String s,int k){
        String []s1=s.split(" ");
        StringBuilder S=new StringBuilder("");
        for (int i = 0; i < k-1; i++) {
            S.append(s1[i]+" ");
        }
        S.append(s1[k-1]);
        //s+=s1[k];
        return String.valueOf(S);
    }
    public static int removeDuplicates(int[] nums) {
        int idx=0;
        for (int i = 0; i < nums.length; i++) {
            if(i+1<nums.length&&nums[i]==nums[i+1]){
                continue;
            }
            else{
                nums[idx++]=nums[i];
            }
        }
        return idx;
    }
    public static void main(String[] args) {
        int arr[]={1,1,2,3};
        int ret=removeDuplicates(arr);
        System.out.println(ret);
        //String s1="Hello how are you Contestant";
        //String str=fun(s1,4);
        //System.out.println(str);
    }
}
